<?php

if (!defined('BASE_PATH'))
	exit('Restricted access');

if (version_compare(PHP_VERSION, '5.0.0', '<'))
	die('require PHP > 5.0 !');

/* 版本号 */
define('VERSION', '1.0 Alpha');
define('FRAME_PATH', dirname(__FILE__));
define('DS', DIRECTORY_SEPARATOR);

/**
 * 系统前端控制器
 * 
 * 加载核心文件，执行请求的控制器方法。实现路由功能
 * 
 * @author sandy
 * @package sFramework
 * 
 */
class frontcontrol {

	protected static $instance = null;
	private static $router = null;
	private static $config = null;
	private static $classname = null;
	private static $action = "index";
	private static $debug = array('start_time' => 0, 'end_time' => 0);
	private static $app = null;
	
	/**
	 * 加载框架必要文件,实例化前端控制器
	 * 
	 * @return object 
	 */
	public static function load() {
		if (defined("SF_DEBUG") && SF_DEBUG) {
			self::$debug['start_time'] = self::get_microtime();
		}
		/*
		 * 加载全局公共函数
		 */
		require_once 'utility.php';
		/*
		 *  加载基础类
		 */
		require_once 'router.class.php';
		require_once 'control.class.php';
		require_once 'model.class.php';

		if (null === self::$instance) {
			self::$instance = new self();
		}
		return self::$instance;
	}
	
	/**
	 * 解析并运行当前请求
	 */
	public static function run() {
		
		self::$config = &load_class('Config');
		self::$router = Router::creatFrontControl();
		self::$classname = self::$router->get_class() . self::$config->get_item('controller_class_ext');
		self::$action = self::$router->get_method();
		
		#TODO 设置时区,PHP环境变量等
		
		$app_file = self::$router->get_class() . '.' . self::$config->get_item('controller_ext');
		$app_file_path = BASE_PATH . DS . self::$config->get_item('controller_folder') . DS . $app_file;

		if (!file_exists($app_file_path)) {
			show_error('The controller file "' . $app_file_path . '" dose not exist.');
		}

		require_once $app_file_path;
		$classname = self::$classname;
		$action = self::$action;
		
		// 实例化当前控制器
		if (class_exists($classname)) {
			self::$app =  new $classname();
			
			if (method_exists(self::$app, $action)) {
				self::$app->setController(self::$router->get_class(),$action);
				self::$app->$action(); // 运行当前请求方法
			} else {
				show_error('The ' . $action . ' method for ' . $classname . ' dose not exists.');
			}
		} else {
			show_error('The class ' . $classname . ' dose not declare.');
		}

		if ( defined("SF_DEBUG") && SF_DEBUG ) {
			self::$debug['end_time'] = self::get_microtime();
			self::show_debug();
		}
	}
	
	/**
	 * 返回当前控制器实例
	 * 
	 * @return object 
	 */
	public static function app(){
		return self::$app;
	}
	
	/**
	 * 获取当前系统时间
	 * 
	 * @return int
	 */
	private static function get_microtime() {
		return microtime(true);
	}
	
	/**
	 * 输出调试信息 
	 */
	private static function show_debug() {
		echo "<ul><li>below is the debug infomation:</li><li>execute time:" . round((self::$debug['end_time'] - self::$debug['start_time']), 5) . "</li></ul>";
	}

}

/**
 * 提供一个缩写 
 */
class SF extends frontcontrol {
	
}

/**
 * 语言类
 * 
 * 获取语言包文件
 * 
 * @author sandy
 * @package sFramework
 *
 */
class Lang {

	private $lang;

	/**
	 * 加载指定的语言文件
	 * 
	 * @param string $file
	 */
	public function load($lang_file) {
		static $loaded_file = array();
		$file_key = md5($lang_file);

		$merge_lang = array();
		if (!isset($loaded_file[$file_key])) {
			$merge_lang = self::fetch($lang_file);
			$loaded_file[$file_key] = $lang_file;
		} else {
			return;
		}

		if (is_array($this->lang)) {
			$merge_lang = array_merge($this->lang, $merge_lang);
		}

		$this->lang = $merge_lang;
	}

	/**
	 * 获取指定key值的语言项内容，未指定key值则返回当前所有语言项内容
	 * 
	 * @param string $key 指定语言项键值
	 */
	public function get($key = '') {
		if (!empty($key)) {
			return isset($this->lang[$key]) ? $this->lang[$key] : $key;
		} else {
			return $this->lang;
		}
	}

	/**
	 *    获取一个语言文件
	 *
	 *    @author    sandy
	 *    @param     string $lang_file
	 *    @return    array
	 */
	protected function fetch($lang_file) {
		$config = &load_class('Config');

		$file_path = BASE_PATH . DS . $config->get_item('language_file_folder') . DS .
				$config->get_item('language') . DS . $lang_file . '.' . $config->get_item('language_file_ext');

		if (file_exists($file_path)) {
			return include($file_path);
		} else {
			show_error(array('the language file ' . $lang_file . ' dose not exist.', 'path:' . $file_path));
		}
	}

}

/**
 * 配置类
 * 
 * 获取应用程序全局配置文件
 * 
 * @author sandy
 * @package sFramework
 *
 */
class Config {

	protected $config;

	public function __construct() {
		$this->_set_config();
	}

	/**
	 * 获取指定的配置项值
	 * 
	 * @param string $item
	 */
	public function get_item($item) {

		if (!isset($this->config[$item])) {
			return false;
		}

		return $this->config[$item];
	}

	/**
	 * 载入配置文件
	 */
	protected function _set_config() {
		if (!isset($this->config)) {
			if (!file_exists(BASE_PATH . DS . 'config.php')) {
				show_error('configuration file dose not exist.');
			}

			$this->config = include (BASE_PATH . DS . 'config.php');
		}
	}

}

/*
class User {
	private $_access = array();
	private $_id;
	private $_username = "游客";
	
	public function __construct() {
		;
	}
}*/